package com.lyshop.jinxiang.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lyshop.jinxiang.R;
import com.lyshop.jinxiang.base.BaseActivity;
import com.lyshop.jinxiang.bean.AddressBean;
import com.lyshop.jinxiang.bean.CreateShopProductBean;
import com.lyshop.jinxiang.bean.GoodsTicketInfoBean;
import com.lyshop.jinxiang.bean.ShopGoodsListBean;
import com.lyshop.jinxiang.bean.UserInfoBean;
import com.lyshop.jinxiang.constants.Constant;
import com.lyshop.jinxiang.net.ActionCallbackListener;
import com.lyshop.jinxiang.utils.DataUtils;
import com.lyshop.jinxiang.utils.GlideUtils;
import com.lyshop.jinxiang.utils.RouteUtil;
import com.lyshop.jinxiang.utils.ToastUtil;
import com.lyshop.jinxiang.view.MoneyTextView;
import com.zc.mychart.utils.ArithUtil;
import com.zc.mychart.utils.FormatUtil;

import java.util.List;

public class HomeAddGoodsOrderActivity extends BaseActivity implements View.OnClickListener {
    private ShopGoodsListBean mGoodsBean;
    private RelativeLayout rl_address;
    private LinearLayout ll_address_info;
    private TextView tv_add_address, tv_user_info, tv_user_address, tv_number, tv_goods_name, tv_right;
    private ImageView iv_goods, iv_sub, iv_add;
    private MoneyTextView mtv_goods_money, mtv_yf, mtv_ticket_money, mtv_total_money;
    private Button btn_submit;
    private int mNumber = 1;//当前数量
    private int mMaxNumber = 100000;//最大数量
    private double mTotalMoney;//总价格
    private double mTicketMoney = 0;//优惠券价格
    private String mTickId = "";//优惠券id
    private String mGoodsid = "";
    private AddressBean mAddressBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mGoodsBean = (ShopGoodsListBean) getIntent().getSerializableExtra(RouteUtil.BUNDLE_HOME_GOODS_MODEL);
        initData();
    }

    private void initData() {
        showProGroupBean();
        getTicket();
        getAddressList();
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_home_add_goods_order;
    }

    @Override
    protected void findViews() {
        rl_address = findViewById(R.id.rl_address);
        ll_address_info = findViewById(R.id.ll_address_info);
        tv_add_address = findViewById(R.id.tv_add_address);
        tv_user_info = findViewById(R.id.tv_user_info);
        tv_user_address = findViewById(R.id.tv_user_address);
        iv_goods = findViewById(R.id.iv_goods);
        tv_goods_name = findViewById(R.id.tv_goods_name);
        mtv_goods_money = findViewById(R.id.mtv_goods_money);
        mtv_yf = findViewById(R.id.mtv_yf);
        mtv_ticket_money = findViewById(R.id.mtv_ticket_money);
        mtv_total_money = findViewById(R.id.mtv_total_money);
        iv_sub = findViewById(R.id.iv_sub);
        iv_add = findViewById(R.id.iv_add);
        tv_number = findViewById(R.id.tv_number);
        tv_right = findViewById(R.id.tv_right);
        btn_submit = findViewById(R.id.btn_submit);
    }

    @Override
    protected void setListeners() {
        iv_add.setOnClickListener(this);
        iv_sub.setOnClickListener(this);
        rl_address.setOnClickListener(this);
        btn_submit.setOnClickListener(this);
        tv_right.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_add:
                if (mNumber < mMaxNumber) {
                    mNumber++;
                }
                setNumber();
                break;
            case R.id.iv_sub:
                if (mNumber == 1) {
                    mNumber = 1;
                } else {
                    mNumber--;
                }
                setNumber();
                break;
            case R.id.rl_address:
                RouteUtil.toAddressListActivity(this, RouteUtil.REQUEST_ADDRESS_LIST);
                break;
            case R.id.btn_submit:
                toBuy();
                break;
            case R.id.tv_right:
                RouteUtil.toWebViewActivity(this, Constant.H5_DELIVERY_RULE);
                break;
        }
    }

    private void toBuy() {
        CreateShopProductBean bean = new CreateShopProductBean();
        if (mAddressBean == null || TextUtils.isEmpty(mAddressBean.id)) {
            ToastUtil.showToast(mContext, "请选择零售订单地址");
            return;
        }
        UserInfoBean userInfo = DataUtils.getUserInfo(mContext);
        if (userInfo == null) return;
        if (mTotalMoney > userInfo.availablebalance){
            ToastUtil.showToast(mContext, "余额不足");
            return;
        }
        bean.address_id = mAddressBean.id;
        bean.amount = mNumber;
        bean.goods_id = mGoodsid;
        bean.couponId = mTickId;
        loading();
        mAppAction.createShopOrder(bean, new ActionCallbackListener<Void>() {
            @Override
            public void onSuccess(Void data) {
                cancelLoading();
                ToastUtil.showToast(mContext, "订单下单成功,进入\"我的->零售订单\"查看");
                RouteUtil.toMainActivity(HomeAddGoodsOrderActivity.this, MainActivity.TAB_HOME);
            }

            @Override
            public void onFailure(String errorCode, String message) {
                cancelLoading();
                ToastUtil.showToast(mContext, message);
            }
        });
    }

    private void showProGroupBean() {
        if (mGoodsBean == null) return;
        mGoodsid = mGoodsBean.goods_id;
        GlideUtils.showImageOptions(mContext, mGoodsBean.goods_img, iv_goods);
        mtv_goods_money.setText(FormatUtil.formatMoney(mGoodsBean.price));
        tv_goods_name.setText(mGoodsBean.name);
        mtv_yf.setText(FormatUtil.formatDouble2(mGoodsBean.freight_amount));
        setNumber();
    }

    /**
     * 设置数量金额
     */
    private void setNumber() {
        if (null != mGoodsBean) {
            mTotalMoney = ArithUtil.mul(mNumber, mGoodsBean.price);
            mTotalMoney = ArithUtil.sub(mTotalMoney, mTicketMoney);
            if (mTotalMoney < 0) {
                mTotalMoney = 0;
            }
            mtv_total_money.setText(FormatUtil.formatDouble2(mTotalMoney));
        }
        tv_number.setText(String.valueOf(mNumber));
    }

    /**
     * 获取零售券
     */
    private void getTicket() {
        mAppAction.getRetailTicket(mGoodsBean.goods_id, 3, new ActionCallbackListener<GoodsTicketInfoBean>() {
            @Override
            public void onSuccess(GoodsTicketInfoBean data) {
                mTickId = data.couponId;
                mTicketMoney = data.faceValue;
                mtv_ticket_money.setText("-" + mTicketMoney);
                setNumber();
            }

            @Override
            public void onFailure(String errorCode, String message) {
//                ToastUtil.showToast(mContext, message);
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null && data.getExtras() != null) {
            if (resultCode == Activity.RESULT_OK) {
                if (requestCode == RouteUtil.REQUEST_ADDRESS_LIST) {
                    mAddressBean = (AddressBean) data.getExtras().get(RouteUtil.BUNDLE_ADDRESS);
                    setAddress();
                }
            }
        }
    }

    private void getAddressList() {
        mAppAction.getAddressList(new ActionCallbackListener<List<AddressBean>>() {
            @Override
            public void onSuccess(List<AddressBean> data) {
                for (AddressBean bean : data) {
                    if (null == mAddressBean && bean.is_default == 1) {
                        mAddressBean = bean;
                    } else if (null != mAddressBean) {
                        if (mAddressBean.id.equals(bean.id)) {
                            mAddressBean = bean;
                        }
                    }
                }
                boolean contains = data.contains(mAddressBean);
                if (!contains) {
                    mAddressBean = null;
                }
                setAddress();
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showToast(mContext, message);
            }
        });
    }

    private void setAddress() {
        if (null != mAddressBean) {
            tv_add_address.setVisibility(View.GONE);
            ll_address_info.setVisibility(View.VISIBLE);

            String user_info = mAddressBean.name + " " + mAddressBean.mobile;
            String address = mAddressBean.region + mAddressBean.address;
            tv_user_info.setText(user_info);
            tv_user_address.setText(address);
        } else {
            tv_add_address.setVisibility(View.VISIBLE);
            ll_address_info.setVisibility(View.GONE);
        }
    }
}